//
//  LFTMCWebViewController.m
//  Pods
//
//  Created by Thomas Quan on 2017/5/16.
//
//

#import "LFTMCWebViewController.h"
#import "SVProgressHUD.h"
#import "NIMGlobalMacro.h"

@interface LFTMCWebViewController () <UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation LFTMCWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    [self.navigationItem setTitle:self.title];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    _webView.scalesPageToFit = YES;
    _webView.delegate = self;
    _webView.backgroundColor = [UIColor clearColor];
    
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":self.userAgent}];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSURLRequest *request = [NSURLRequest requestWithURL:_url];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
}

- (void)close {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupNav {
    UIBarButtonItem *backButtonItem =
    [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"LFTMC_Back"]
                                     style:UIBarButtonItemStylePlain
                                    target:self
                                    action:@selector(close)];
    [backButtonItem setTintColor:NIMKit_UIColorFromRGB(0x333333)];
    self.navigationItem.leftBarButtonItem = backButtonItem;
}

- (void)viewWillDisappear:(BOOL)animated {
    [SVProgressHUD dismiss];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIWebViewDelegate methods

- (BOOL)webView:(__unused UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType {
    
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [SVProgressHUD show];
}

- (void)webView:(__unused UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [SVProgressHUD dismiss];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [SVProgressHUD dismiss];
}


@end
